package org.example.warlock;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.IntFunction;
import java.util.stream.IntStream;

public class TestWarlock {


    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);


        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet getRequest = new HttpGet("https://api-phx-di-ali.yunxuetang.com.cn/tlive/dev/warlock/test1");
        getRequest.setHeader("token", "eyJhbGciOiJIUzUxMiJ9.eyJvcmdJZCI6ImRjOWRlMzkxLWU0ZTktNDcwNC1hODgxLWJhN2I3NGEzN2U1NyIsInVzZXJJZCI6IjJmYTQwOWU4LTg0MWEtNDAzMy05NzJjLWNlNWIyZjA3NDU1MyIsImNsdXN0ZXJJZCI6InYyZGV2IiwiZXhwIjoxNjgyMzg5MDQzfQ._TP_z4659R8vxw_-d9_nuh01OcH04sTYtnIpgp6wFoPQUIwhJTLyJ0FGIGZJbdW8OXithXmakq-5v0GU5v-cKQ");

        LocalDateTime startTime = LocalDateTime.now();
        IntStream.range(0, 100)
                .mapToObj((IntFunction<Future<?>>) value -> executorService.submit(() -> {
                    try {
                        httpClient.execute(getRequest);
                        LocalDateTime endTime = LocalDateTime.now();
                        long between = ChronoUnit.SECONDS.between(startTime, endTime);
                        System.out.println(value + " -- " + between + "秒");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })).forEach(future -> {
                    try {
                        future.get();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } catch (ExecutionException e) {
                        throw new RuntimeException(e);
                    }
                });
    }
}
